package com.upb.webserver.node;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.node.RspNodeVO;
import com.upb.webserver.common.pojo.order.ReqAddNewNode2ChainVO;
import com.upb.webserver.common.pojo.order.RespAddNodeVO;
import com.upb.webserver.common.pojo.order.RspChainDeployVO;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.order.OrderService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * work order api, support close order
 */
@Log4j2
@RestController
@RequestMapping("node")
public class NodeController {
    @Autowired
    private NodeService nodeService;
    @Autowired
    private AccountService accountService;


    /**
     * @param chainId
     * @param applicationId
     * @return
     */
    @GetMapping(value = "list")
    public BasePageResponse queryNodeList(@RequestParam(value = "chainId") Integer chainId,
                                          @RequestParam(value = "applicationId", required = false) Integer applicationId,
                                          @RequestParam(value = "enableState", required = false, defaultValue = "false") Boolean enableState) {
        log.info("start exec method [queryNodeList]. chainId:{} applicationId:{} enableState:{}", chainId, applicationId, enableState);
        List<RspNodeVO> nodeList = nodeService.queryNodeList(chainId, applicationId, enableState);
        BasePageResponse baseResponse = new BasePageResponse(ConstantCode.SUCCESS);
        baseResponse.setData(nodeList);
        baseResponse.setTotalCount(CollectionUtils.size(nodeList));
        log.info("success exec method [queryNodeList]. result:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }


    @Autowired
    private OrderService orderService;

    //新建节点工单
    @PostMapping(value = "work-order-of-new-node")
    public BaseResponse addWorkOrderOfNewNode(@RequestBody @Valid ReqAddNewNode2ChainVO reqAddNewNode2ChainVO, HttpServletRequest request) {
        log.info("start exec method [addWorkOrderOfNewNode]. param:{}", JsonUtils.objToString(reqAddNewNode2ChainVO));
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        BaseResponse baseResponse = orderService.addWorkOrderOfNewNode(accountCommon, reqAddNewNode2ChainVO);
        log.info("success exec method [addWorkOrderOfNewNode]. baseResponse:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }

}
